/*
 * Copyright 2013 Sigurd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.shared.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Sigurd
 */
public class ObjectHelper {

    private static final String NULL_VALUE = "NULL";
    private static final String DATE_FORMAT = "d.M.y H:m:s:S";

    private ObjectHelper() {
    }

    public static String objectToString(Object obj) {
        if (obj == null) {
            return NULL_VALUE;
        } else if (obj instanceof File) {
            return ((File) obj).getAbsolutePath();
        } else if (obj instanceof Date) {
            SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
            return format.format(obj);
        } else if (obj instanceof Class) {
            return ((Class) obj).getCanonicalName();
        }
        return obj.toString();

    }

    public static <T> T stringToObject(Class<T> clazz, String value) {
        try {
            if (value.equals(NULL_VALUE)) {
                return null;
            } else if (Boolean.class.isAssignableFrom(clazz)) {
                return (T) Boolean.valueOf(value);
            } else if (Integer.class.isAssignableFrom(clazz)) {
                return (T) Integer.valueOf(value);
            } else if (Long.class.isAssignableFrom(clazz)) {
                return (T) Long.valueOf(value);
            } else if (Float.class.isAssignableFrom(clazz)) {
                return (T) Float.valueOf(value);
            } else if (Double.class.isAssignableFrom(clazz)) {
                return (T) Double.valueOf(value);
            } else if (String.class.isAssignableFrom(clazz)) {
                return (T) value;
            } else if (File.class.isAssignableFrom(clazz)) {
                return (T) new File(value);
            } else if (URL.class.isAssignableFrom(clazz)) {
                return (T) new URL(value);
            } else if (Date.class.isAssignableFrom(clazz)) {
                SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
                return (T) format.parse(value);
            } else if (Path.class.isAssignableFrom(clazz)) {
                return (T) Paths.get(value);
            } else if (Enum.class.isAssignableFrom(clazz)) {
                return (T) Enum.valueOf((Class<Enum>) clazz, value);
            } else if (Class.class.equals(clazz)) {
                return (T) Class.forName(value);
            }
        } catch (MalformedURLException | RuntimeException | ParseException | ClassNotFoundException ex) {
            throw new IllegalStateException("Error while converting value:" + value + " to class: " + clazz, ex);
        }
        throw new IllegalArgumentException("Cannot convert value " + value + " to class " + clazz);
    }
}
